package com.xgh.offer;

/**
 * @ClassName Offerr53_2
 * @Description
 * @Author xinggh
 * @Date 2020/7/3 9:24
 * @Version 1.0
 **/
public class Offerr59 {
    /**
     * 剑指 Offer 59 - I. 滑动窗口的最大值
     * 给定一个数组 nums 和滑动窗口的大小 k，请找出所有滑动窗口里的最大值。
     * <p>
     * 示例:
     * <p>
     * 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
     * 输出: [3,3,5,5,6,7]
     * 解释:
     * <p>
     * 滑动窗口的位置                最大值
     * ---------------               -----
     * [1  3  -1] -3  5  3  6  7       3
     * 1 [3  -1  -3] 5  3  6  7       3
     * 1  3 [-1  -3  5] 3  6  7       5
     * 1  3  -1 [-3  5  3] 6  7       5
     * 1  3  -1  -3 [5  3  6] 7       6
     * 1  3  -1  -3  5 [3  6  7]      7
     */

    public static int[] maxSlidingWindow(int[] nums, int k) {

        if(nums.length == 0){
            return new int[0];
        }
        int[] res = new int[nums.length - k + 1];
        int i = 0;
        int j = k - 1;
        while (j < nums.length) {
            res[i] = max(nums, i, j);
            i++;
            j++;
        }
        return res;

    }

    public static int max(int[] nums, int start, int end) {
        int max = nums[start];
        for (int i = start; i <= end; i++) {
            max = Math.max(max, nums[i]);

        }
        return max;
    }

    public static void main(String[] args) {
        int[] num = {1, 3, -1, -3, 5, 3, 6, 7};
        maxSlidingWindow(num, 3);
    }

}
